
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 04/19/2011 10:02:38
-- Generated from EDMX file: d:\documents\visual studio 2010\Projects\WPFEntityFrameworkTest\WPFDataModel\DataModel\DataModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [EntityTestDB01];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_PersonCategory]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonSet] DROP CONSTRAINT [FK_PersonCategory];
GO
IF OBJECT_ID(N'[dbo].[FK_GroupCategory]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GroupSet] DROP CONSTRAINT [FK_GroupCategory];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonGroup_Person]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonGroup] DROP CONSTRAINT [FK_PersonGroup_Person];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonGroup_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonGroup] DROP CONSTRAINT [FK_PersonGroup_Group];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[PersonSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonSet];
GO
IF OBJECT_ID(N'[dbo].[GroupSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[GroupSet];
GO
IF OBJECT_ID(N'[dbo].[CategorySet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CategorySet];
GO
IF OBJECT_ID(N'[dbo].[PersonGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonGroup];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'PersonSet'
CREATE TABLE [dbo].[PersonSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [FirstName] nvarchar(max)  NOT NULL,
    [Category_Id] int  NOT NULL
);
GO

-- Creating table 'GroupSet'
CREATE TABLE [dbo].[GroupSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Category_Id] int  NOT NULL
);
GO

-- Creating table 'CategorySet'
CREATE TABLE [dbo].[CategorySet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [IsSelected] bit  NULL
);
GO

-- Creating table 'PersonGroup'
CREATE TABLE [dbo].[PersonGroup] (
    [Persons_Id] int  NOT NULL,
    [Groups_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'PersonSet'
ALTER TABLE [dbo].[PersonSet]
ADD CONSTRAINT [PK_PersonSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'GroupSet'
ALTER TABLE [dbo].[GroupSet]
ADD CONSTRAINT [PK_GroupSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'CategorySet'
ALTER TABLE [dbo].[CategorySet]
ADD CONSTRAINT [PK_CategorySet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Persons_Id], [Groups_Id] in table 'PersonGroup'
ALTER TABLE [dbo].[PersonGroup]
ADD CONSTRAINT [PK_PersonGroup]
    PRIMARY KEY NONCLUSTERED ([Persons_Id], [Groups_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Category_Id] in table 'PersonSet'
ALTER TABLE [dbo].[PersonSet]
ADD CONSTRAINT [FK_PersonCategory]
    FOREIGN KEY ([Category_Id])
    REFERENCES [dbo].[CategorySet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonCategory'
CREATE INDEX [IX_FK_PersonCategory]
ON [dbo].[PersonSet]
    ([Category_Id]);
GO

-- Creating foreign key on [Category_Id] in table 'GroupSet'
ALTER TABLE [dbo].[GroupSet]
ADD CONSTRAINT [FK_GroupCategory]
    FOREIGN KEY ([Category_Id])
    REFERENCES [dbo].[CategorySet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GroupCategory'
CREATE INDEX [IX_FK_GroupCategory]
ON [dbo].[GroupSet]
    ([Category_Id]);
GO

-- Creating foreign key on [Persons_Id] in table 'PersonGroup'
ALTER TABLE [dbo].[PersonGroup]
ADD CONSTRAINT [FK_PersonGroup_Person]
    FOREIGN KEY ([Persons_Id])
    REFERENCES [dbo].[PersonSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Groups_Id] in table 'PersonGroup'
ALTER TABLE [dbo].[PersonGroup]
ADD CONSTRAINT [FK_PersonGroup_Group]
    FOREIGN KEY ([Groups_Id])
    REFERENCES [dbo].[GroupSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonGroup_Group'
CREATE INDEX [IX_FK_PersonGroup_Group]
ON [dbo].[PersonGroup]
    ([Groups_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------